<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加机构</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/css/oksub.css">
    <script type="text/javascript" src="/static/lib/loading/okLoading.js"></script>
    <script src="/static/lib/layui/layui.js"></script>

    <link type="text/css" rel="stylesheet" href="/static/lib/layui/treeSelect/metroStyle.css">
    <link type="text/css" rel="stylesheet" href="/static/lib/layui/treeSelect/treeSelectStyle.css">
    <script src="/static/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.ztree.all.js"></script>
    <script type="text/javascript" src="/static/js/treeSelect.2.0.js"></script>
    <script type="text/javascript" src="/static/js/xm-select.js"></script>
    <script type="text/javascript" src="/static/js/xeutils.js"></script>
</head>

<body>
    <div class="ok-body">
        <!--form表单-->
        <form class="layui-form layui-form-pane ok-form">
            {% csrf_token %}
            <div class="layui-form-item">
                <label class="layui-form-label">机构代码<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="key" placeholder="请输入机构/部门代码，且不重复！" autocomplete="off"
                        class="layui-input" maxlength="18" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">机构名称<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="name" placeholder="请输入机构/部门名称" autocomplete="off" class="layui-input"
                        lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属机构</label>
                <div class="layui-input-block" id="parent" name="parent">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">负责人</label>
                <div class="layui-input-block">
                <input type="text" name="owner" placeholder="请输入负责人姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-block">
                <input type="text" name="phone" lay-verify="phone"  placeholder="请输入电话号码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                <input type="text" name="email" lay-verify="email" placeholder="请输入邮箱地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                <input type="text" name="sort" lay-verify="int" placeholder="请输入数字排序" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="description" placeholder="请输入备注内容/140个汉字" maxlength="255"
                        class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="add"><span class="ok-icon">&#xe645;</span>
                        立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary" id="close"><span class="ok-icon">&#xe646;</span>
                        关闭</button>
                </div>
            </div>
        </form>
    </div>
    <!--js逻辑-->
    <script>
        layui.use(["form", "okUtils", "form", "okLayer"], function () {
            let $ = layui.$;
            let form = layui.form;
            let okUtils = layui.okUtils;
            let okLayer = layui.okLayer;
            let tree = layui.tree;
            okLoading.close();

            // 这里得把switch的状态弄对，不然，如果他创建就停用，数据就传不过去了
            form.on("submit(add)", function (data) {
                okUtils.ajax("{% url 'system:addDeptData' %}", "post", data.field, true).done(function (response) {
                    okLayer.greenTickMsg(response.msg, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }).fail(function (error) {
                    // 这里已经有提示了
                });
                return false;
            });

        var demo3 = xmSelect.render({
            el: '#parent',
            prop: {
                name: 'title',
                value: 'id',
            },
            model: { label: { type: 'text' } },
            radio: true,
            clickClose: true,
            tree: {
                show: true,
                strict: false,
                expandedKeys: [-1],
            },
            height: 'auto',
            data() {
                return []
            }
        });
        $("#close").click(function () {
            let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
            parent.layer.close(index);

        });
        okUtils.ajax("{% url 'system:getDeptDataTree' %}", "get", "", true).done(function (response) {
            var node = response;
                demo3.update({
                    data: XEUtils.toArrayTree(node.data,{ id:'id',parentKey: 'parent', strict: true, children:'children' }),
                    autoRow: true,
            })
        });
    });
    </script>
</body>

</html>